package com.sgcc.common.core.web.page;

import com.sgcc.common.core.utils.CookieUtils;
import com.sgcc.common.core.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 分页数据
 * 
 * @author sgcc
 */
public class PageDomain<T>
{
    /** 当前记录起始索引 */
    private Integer pageNum;

    /** 每页显示记录数 */
    private Integer pageSize;

    /** 排序列 */
    private String orderByColumn;

    /** 排序的方向desc或者asc */
    private String isAsc = "asc";

    private String orderBy = ""; // 标准查询有效， 实例： updatedate desc, name asc

    public String getOrderBy()
    {
        if (StringUtils.isEmpty(orderByColumn))
        {
            return "";
        }
        return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc;
    }

    /**
     * 构造方法
     */
    public PageDomain(){
    }

    /**
     * 构造方法
     * @param request 传递 repage 参数，来记住页码
     * @param response 用于设置 Cookie，记住页码
     */
    public PageDomain(HttpServletRequest request, HttpServletResponse response){
        this(request, response, -2);
    }

    /**
     * 构造方法
     * @param request 传递 repage 参数，来记住页码
     * @param response 用于设置 Cookie，记住页码
     * @param defaultPageSize 默认分页大小，如果传递 -1 则为不分页，返回所有数据
     */
    public PageDomain(HttpServletRequest request, HttpServletResponse response, int defaultPageSize){
        // 设置页码参数（传递repage参数，来记住页码）
        String no = request.getParameter("pageNum");
        if (org.apache.commons.lang3.StringUtils.isNumeric(no)){
            CookieUtils.setCookie(response, "pageNum", no);
            this.setPageNum(Integer.parseInt(no));
        }else if (request.getParameter("repage")!=null){
            no = CookieUtils.getCookie(request, "pageNum");
            if (org.apache.commons.lang3.StringUtils.isNumeric(no)){
                this.setPageNum(Integer.parseInt(no));
            }
        }
        // 设置页面大小参数（传递repage参数，来记住页码大小）
        String size = request.getParameter("pageSize");
        if (org.apache.commons.lang3.StringUtils.isNumeric(size)){
            CookieUtils.setCookie(response, "pageSize", size);
            this.setPageSize(Integer.parseInt(size));
        }else if (request.getParameter("repage")!=null){
            no = CookieUtils.getCookie(request, "pageSize");
            if (org.apache.commons.lang3.StringUtils.isNumeric(size)){
                this.setPageSize(Integer.parseInt(size));
            }
        }else if (defaultPageSize != -2){
            this.pageSize = defaultPageSize;
        }else if ("-1".equals(size)){
            this.pageSize = -1;
        }
        // 设置排序参数
        String orderBy = request.getParameter("orderBy");
        if (org.apache.commons.lang3.StringUtils.isNotBlank(orderBy)){
            this.setOrderBy(orderBy);
        }
    }

    /**
     * 设置页面大小（最大500）
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize <= 0 ? 10 : pageSize;// > 500 ? 500 : pageSize;
    }

    /**
     * 设置查询排序，标准查询有效， 实例： updatedate desc, name asc
     */
    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    /**
     * 设置当前页码
     * @param pageNum
     */
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageNum()
    {
        return pageNum;
    }

    public void setPageNum(Integer pageNum)
    {
        this.pageNum = pageNum;
    }

    public Integer getPageSize()
    {
        return pageSize;
    }

    public void setPageSize(Integer pageSize)
    {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn()
    {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn)
    {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc()
    {
        return isAsc;
    }

    public void setIsAsc(String isAsc)
    {
        this.isAsc = isAsc;
    }
}
